package com.dolphin.common.utils;

import org.omg.CORBA.ULongLongSeqHelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author shida
 */
public class DateUtils {

    public static final String YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";

    public static final String YYYYMMDDHHMMSSSSS = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String YYYYMMDDHH = "yyyyMMddHH";
    public static final String HH = "HH";

    public static String deterMineDateformatByContent(String str){
        try{
            char[] cs = "yyyyMMddHHmmssSSSSS".toCharArray();
            StringBuilder sb = new StringBuilder(str.length());
            int i = 0;
            for(char c : str.toCharArray()){
                if(c >= '0' && c <= '9'){
                    if(i > cs.length){
                        throw new RuntimeException("Can not determine format:" + str);
                    }
                    sb.append(cs[i++]);
                }else {
                    sb.append(c);
                }
            }

            return sb.toString();
        }catch (Exception ex){
            throw new RuntimeException("Can not determine format:" + str);
        }
    }

    public static Date parseDate(String str, String format){
        try {
            return new SimpleDateFormat(format).parse(str);
        } catch (ParseException e) {
            return null;
        }
    }

    public static String dateToString(Date date, String format){
        return new SimpleDateFormat(format).format(date);
    }

}